package com.easybuilder.core.table.controller;

import com.alibaba.fastjson.JSONObject;
import com.easybuilder.common.base.controller.BaseController;
import com.easybuilder.common.base.entity.DynamicEntity;
import com.easybuilder.common.utils.StringUtil;
import com.easybuilder.core.table.dto.TableTree;
import com.easybuilder.core.table.entity.SysTable;
import com.easybuilder.core.table.service.TableService;
import org.apache.catalina.util.LifecycleBase;
import org.apache.ibatis.annotations.Delete;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * @author ZXS
 * @version 1.0
 * @description 类
 * @date 2021-5-25 23:26
 */
@RestController
@RequestMapping("/sys/table")
public class TableController extends BaseController {
    @Resource
    private TableService tableService;

    @GetMapping("/{id}")
    public Result<Object> getTable(@PathVariable Long id) {
        SysTable table = tableService.getTableById(id);
        return OK(table);
    }
    /**
     * @Author zhangxingshuai
     * @Description //TODO 查询模块或表的树形结构
     * @Date 10:41 2021/8/23
     * @Param []
     * @return com.easybuilder.common.base.controller.BaseController.Result
     **/
    @GetMapping("/tree")
    public Result selectList() {
        List<TableTree> result = tableService.getTree();
        return OK(result);
    }


    /**
     * @Author zhangxingshuai
     * @Description //TODO 保存模块或表
     * @Date 11:27 2021/8/23
     * @Param [sysTable, bindingResult]
     * @return com.easybuilder.common.base.controller.BaseController.Result<java.lang.Object>
     **/
    @PostMapping("/save")
    @Transactional
    public Result<Object> createTable(@Valid @RequestBody SysTable sysTable, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String message = bindingResult.getFieldError().getDefaultMessage();
            return Error(HttpStatus.BAD_REQUEST, message);
        }
        SysTable table = null;
        synchronized (this) {
            boolean exist = tableService.checkModelCodeExist(sysTable);
            if (exist) {
                String message = (sysTable.getType() == 0 ? "模块" : "表") + "编码重复！";
                return Error(message);
            } else {
                table = tableService.save(sysTable);
                if (table.getId() != null) {
                    return OK(table, "保存成功！");
                }
            }
        }
        return OK(table, "保存失败，请联系管理员！");
    }


    /**
     * @Author zhangxingshuai
     * @Description //TODO 删除表
     * @Date 15:41 2021/8/23
     * @Param [id]
     * @return com.easybuilder.common.base.controller.BaseController.Result<java.lang.Object>
     **/
    @DeleteMapping("/delete/{id}")
    public Result<Object> delete(@PathVariable(name = "id", required = true) Long id) {
        int delete = tableService.delete(id);
        if (delete > 0) {
            return OK("删除成功");
        }
        return Error("删除失败");
    }


    /**
     * @Author zhangxingshuai
     * @Description //TODO 创建数据库表结构
     * @Date 16:01 2021/8/23
     * @Param [sysTable]
     * @return com.easybuilder.common.base.controller.BaseController.Result<java.lang.Object>
     **/
    @PostMapping("/create")
    public Result<Object> create(@RequestBody SysTable sysTable) {
        if (sysTable.getType() == 0L) {
            return Error("模块不可创建表结构");
        }
        if (sysTable.getId() == null) {
            return Error("需保存数据后创建");
        }
        boolean b = tableService.create(sysTable);
        if (b) {
            return OK("表结构创建成功");
        }
        return Error("表结构创建失败");
    }

    /**
     * @Author zhangxingshuai
     * @Description //TODO 修改数据库表结构
     * @Date 16:01 2021/8/23
     * @Param [sysTable]
     * @return com.easybuilder.common.base.controller.BaseController.Result<java.lang.Object>
     **/
    @PostMapping("/alter")
    public Result<Object> alter(@RequestBody SysTable sysTable) {
        boolean success = tableService.alter(sysTable);
        if (success) {
            return OK("操作成功");
        }
        return Error("操作失败");
    }

    /**
     * @Author zhangxingshuai
     * @Description //TODO 删除数据库表结构
     * @Date 16:01 2021/8/23
     * @Param [sysTable]
     * @return com.easybuilder.common.base.controller.BaseController.Result<java.lang.Object>
     **/
    @PostMapping("/drop")
    public Result<Object> drop(@RequestBody SysTable sysTable) {
        boolean success = tableService.drop(sysTable);
        if (success) {
            return OK("删除成功");
        }
        return Error("删除失败");
    }

}
